﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using DiscoveryLogic.Analytics.BusinessLogic;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.UI.Common.Events;
using DiscoveryLogic.Common.Utilities;
using Microsoft.Practices.Composite.Events;

namespace DiscoveryLogic.Analytics.Modules.DataNavigation
{
    public class EntitySetsTreePresenter
    {
        private EntitySetsTreeView _View;
		/// <summary></summary>
		public EntitySetsTreeView View
		{
			get
			{
				return this._View;
			}
			set
			{
				this._View = value;
				if(this._View !=null)
				{
					this._View.EntityDataSetSelected += new EntitySetsTreeView.OnEntityDataSetSelected(SelectEntityDataSet);
				    this._View.PopulateTreeView(this.ReadEntityDataSets());
				}
			}
		}

		private readonly IEventAggregator _EventAggregator;

        public EntitySetsTreePresenter(IEventAggregator eventAggregator)
		{
			this._EventAggregator = eventAggregator;
		}

        public void SelectEntityDataSet(EntityDataSet entitySet)
		{
            if (entitySet != null)
			{
                switch(entitySet.EntityType)
                {
                    case EntityType.Grants:
                        this._EventAggregator.GetEvent<GrantDataSetSelectedEvent>().Publish(entitySet.DataSetName);
                        break;
                    case EntityType.Organizations:
                        this._EventAggregator.GetEvent<OrgDataSetSelectedEvent>().Publish(entitySet.DataSetName);
                        break;
                    case EntityType.Persons:
                        this._EventAggregator.GetEvent<PersonDataSetSelectedEvent>().Publish(entitySet.DataSetName);
                        break;
                    case EntityType.Publications:
                        this._EventAggregator.GetEvent<PubDataSetSelectedEvent>().Publish(entitySet.DataSetName);
                        break;
                    case EntityType.Document:
                        this._EventAggregator.GetEvent<DocDataSetSelectedEvent>().Publish(entitySet.DataSetName);
                        break;
                    case EntityType.Subject:
                        this._EventAggregator.GetEvent<SubjDataSetSelectedEvent>().Publish(entitySet.DataSetName);
                        break;
                }
			}
		}

        private ObservableCollection<EntitySetsByType> ReadEntityDataSets()
        {
            //ObservableCollection<NetworksBySource> allNetworks=new ObservableCollection<NetworksBySource>();

            //List<NetworkDefinition> dbNetworks = NetworkManager.GetNetworkDefs(SetDataSourceType.DB, "../images/database.ico");
            //allNetworks.Add(new NetworksBySource(SetDataSourceType.DB.ToString(), dbNetworks));

            //List<NetworkDefinition> xlNetworks = NetworkManager.GetNetworkDefs(SetDataSourceType.ExcelFile, "../images/excel.ico");
            //allNetworks.Add(new NetworksBySource(SetDataSourceType.ExcelFile.ToString(), xlNetworks));

            //return allNetworks;
            MyData.ReadAllEntitySets();
            return MyData.AllEntitySets;
        }
    }
}
